<template>
  <div class="folder-tree">
    <div class="folder-tree-header">
      <el-input
        v-model="searchQuery"
        placeholder="搜索笔记..."
        :prefix-icon="Search"
        clearable
      />
      <el-button-group class="mt-2">
        <el-button :icon="Plus" @click="createNote">新建笔记</el-button>
        <el-button :icon="FolderAdd" @click="createFolder">新建文件夹</el-button>
      </el-button-group>
    </div>

    <el-tree
      :data="folders"
      :props="defaultProps"
      node-key="id"
      draggable
      :allow-drop="allowDrop"
      @node-drag-end="handleDragEnd"
      @node-click="handleNodeClick"
    >
      <template #default="{ node, data }">
        <div class="custom-tree-node">
          <span>
            <el-icon><Folder /></el-icon>
            {{ node.label }}
          </span>
          <span class="actions">
            <el-dropdown trigger="click" @command="handleCommand($event, data)">
              <el-button type="text" :icon="More" />
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="rename">重命名</el-dropdown-item>
                  <el-dropdown-item command="delete" divided>删除</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </span>
        </div>
      </template>
    </el-tree>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Search, Plus, FolderAdd, More, Folder } from '@element-plus/icons-vue'
import type { NoteFolder } from '@/types'
import { useNoteStore } from '@/stores/note'
import { useRouter } from 'vue-router'

const router = useRouter()
const noteStore = useNoteStore()
const searchQuery = ref('')
const folders = ref<NoteFolder[]>([])

const defaultProps = {
  children: 'children',
  label: 'name'
}

const createNote = () => {
  const note = noteStore.createNote('markdown', 'root')
  router.push(`/note/${note.id}`)
}

const createFolder = () => {
  noteStore.createFolder('新建文件夹')
}

const allowDrop = (draggingNode: any, dropNode: any, type: string) => {
  return type !== 'inner' || dropNode.data.type === 'folder'
}

const handleDragEnd = (draggingNode: any, dropNode: any, dropType: string) => {
  if (!dropNode || !draggingNode) return
  
  const dragData = draggingNode.data
  const dropData = dropNode.data
  
  if (dropType === 'inner') {
    noteStore.updateFolder(dragData.id, { parent: dropData.id })
  } else {
    noteStore.updateFolder(dragData.id, { parent: dropData.parent })
  }
}

const handleNodeClick = (data: NoteFolder) => {
  // TODO: 实现文件夹点击逻辑
}

const handleCommand = (command: string, data: NoteFolder) => {
  switch (command) {
    case 'rename':
      // TODO: 实现重命名功能
      break
    case 'delete':
      noteStore.deleteFolder(data.id)
      break
  }
}
</script>

<style lang="scss" scoped>
.folder-tree {
  height: 100%;
  display: flex;
  flex-direction: column;

  &-header {
    padding: 16px;
  }

  .el-tree {
    flex: 1;
    padding: 0 16px;
    background: transparent;
  }

  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;

    .el-icon {
      margin-right: 8px;
      font-size: 16px;
    }

    .actions {
      opacity: 0;
      transition: opacity 0.2s;
    }

    &:hover .actions {
      opacity: 1;
    }
  }
}

.mt-2 {
  margin-top: 8px;
}
</style>
